package com.kd.juhaowan.ui;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.Window;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;


import com.hjq.http.listener.OnHttpListener;
import com.hjq.toast.ToastUtils;
import com.kd.juhaowan.base.BoxCacheManager;
import com.kd.juhaowan.http.api.exception.TokenException;
import com.kd.juhaowan.http.model.HttpData;
import com.kd.juhaowan.utils.LogUtil;
import com.kd.juhaowan.utils.StatusBarUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import butterknife.ButterKnife;
import butterknife.Unbinder;

public abstract class BaseRefreshActivity extends AppCompatActivity implements OnHttpListener , OnRefreshListener, OnLoadMoreListener{
    private Unbinder mUnbinder;
    BoxCacheManager manager;
    private int mCurrentPageCount = 0;
    private boolean isRefresh;
    protected static final int ITEM_VIEW_TYPE_Title = 1;
    protected static final int ITEM_VIEW_TYPE_Info = 2;
    private ActivityResultLauncher<Intent> baseRegister;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StatusBarUtil.setDarkMode(this);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

            View decorView=getWindow().getDecorView();

            int option=View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;

            decorView.setSystemUiVisibility(option);

            getWindow().setStatusBarColor(Color.TRANSPARENT);

        }
        baseRegister = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
            @Override
            public void onActivityResult(ActivityResult result) {
                LogUtil.debugLog( "onActivityResult: _____" + result);
                if (result != null) {

                    if (result.getResultCode() == Activity.RESULT_OK) {
                        reInitDate();

                    }
                }
            }
        });
        setContentView(getViewLayoutResourceId());
        mUnbinder = ButterKnife.bind(this);
        manager = BoxCacheManager.getInstence(this);
        getSmartRefreshLayout().setEnableLoadMore(true);
        getSmartRefreshLayout().setOnRefreshListener(this);
        getSmartRefreshLayout().setOnLoadMoreListener(this);
    }

    protected abstract SmartRefreshLayout getSmartRefreshLayout();

    protected abstract String getSmartRefreshTaskTag();

    protected abstract void onRequestNextPageData(int page);

    protected abstract boolean isNullData(HttpData response);

    protected abstract int getViewLayoutResourceId();
    protected abstract void reInitDate();

    @Override
    public void onHttpSuccess(Object result) {
        mCurrentPageCount = isRefresh ? 1 : mCurrentPageCount + 1;
        finishRefreshOrLoadMore(true, false);
        LogUtil.debugLog("base success");
    }

    @Override
    public void onHttpFail(Exception e) {
        LogUtil.debugLog("base faild"+e.getMessage());
        if(e instanceof TokenException){

            manager.writeAutoLogin(false);
            baseRegister.launch(new Intent(BaseRefreshActivity.this,LoginAccountActivity.class));
//            startActivity();

//            finish();
        }else {
            ToastUtils.show(e.getMessage());

            LogUtil.debugLog("base faild"+e.getMessage());

        }



    }

    public void finishRefreshOrLoadMore(boolean success, boolean noMoreData) {
        if (getSmartRefreshLayout() != null) {
            if (isRefresh) {
                getSmartRefreshLayout().finishRefresh(DEFAULT_DELAYED_SECOND, success, noMoreData);
            } else {
                getSmartRefreshLayout().finishLoadMore(DEFAULT_DELAYED_SECOND, success, noMoreData);
            }
        }
    }
    private static final int DEFAULT_DELAYED_SECOND = 0;
    @Override
    public void onLoadMore(RefreshLayout refreshLayout) {
        this.isRefresh = false;
        onRequestNextPageData(mCurrentPageCount + 1);
    }

    @Override
    public void onRefresh(RefreshLayout refreshLayout) {
        this.isRefresh = true;
        onRequestNextPageData(1);
    }
}
